<!DOCTYPE html>
<html lang="en-us">
<head><head>
    <meta name="google-site-verification" content="9vIieCe-Qpd78QOmBl63rGtIVbhY6sYyuxX3j8XWBA4" />
    <meta name="baidu-site-verification" content="LRrmH41lz7" />
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="google-site-verification" content="xBT4GhYoi5qRD5tr338pgPM5OWHHIDR6mNg1a3euekI" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
    <meta name="baidu-site-verification" content="HGLXRsUXC4" />

    
    <meta name="baidu-site-verification" content="code-ANZvlnN0Xr" />

    
    <meta name="description" content="随着云原生的发展,业务规模的壮大,yml的维护会越来越复杂,这时候提高效能的工具或语言就应运而生了,ballerina便是其一.">
    
    <meta name="keyword"  content="liangyuanpeng|LanLiang|OpenYurt|Knative|Pulsar|Prometheus|Halo||边缘计算kubernetes|Docker|CloudNative|Golang|Rust|Istio|微服务">
    <link rel="shortcut icon" href="img/logo.png">


    
    <meta property="og:image" content="https://res.cloudinary.com/lyp/image/upload/v1544363191/hugo/blog.github.io/743a4e9227e1f14cb24a1eb6db29e183.jpg" />
    <meta name="twitter:image" content="https://res.cloudinary.com/lyp/image/upload/v1544363191/hugo/blog.github.io/743a4e9227e1f14cb24a1eb6db29e183.jpg" />


    <title>云原生编程语言ballerina:hello-world-liangyuanpeng的博客 | liangyuanpeng&#39;s Blog</title>

    <link rel="canonical" href="/post/ballerina-hello-world/">

    <link rel="stylesheet" href="https://res.cloudinary.com/lyp/raw/upload/v1537369740/hugo/css/iDisqus.min.css"/>

    
    <link rel="stylesheet" href="https://res.cloudinary.com/lyp/raw/upload/v1537369744/hugo/css/bootstrap.min.css">

    
    <link rel="stylesheet" href="https://res.cloudinary.com/lyp/raw/upload/v1537369740/hugo/css/hux-blog.min.css">

    
    <link rel="stylesheet" href="https://res.cloudinary.com/lyp/raw/upload/v1537369740/hugo/css/syntax.css">

    
    <link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    
    
    <script src="https://res.cloudinary.com/lyp/raw/upload/v1537369966/hugo/js/jquery.min.js"></script>

    
    <script src="https://res.cloudinary.com/lyp/raw/upload/v1537369966/hugo/js/bootstrap.min.js"></script>

    
    <script src="https://res.cloudinary.com/lyp/raw/upload/v1537369964/hugo/js/hux-blog.min.js"></script>

    
    <script defer src='https://static.cloudflareinsights.com/beacon.min.js' data-cf-beacon='{"token": "f73bc0092aeb491d89984c0eb5a87ac2"}'></script>
</head>

</head>

<nav class="navbar navbar-default navbar-custom navbar-fixed-top">
    <div class="container-fluid">
        
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="/">Hi,I`m lan</a>
        </div>

        
        
        <div id="huxblog_navbar">
            <div class="navbar-collapse">
                <ul class="nav navbar-nav navbar-right">
                    <li>
                        <a href="/">Home</a>
                    </li>
                    
                    <li>
                        <a href="categories/cloudnative">cloudnative</a>
                    </li>
                    
                    <li>
                        <a href="categories/devops">devops</a>
                    </li>
                    
                    <li>
                        <a href="categories/iot">iot</a>
                    </li>
                    
                    <li>
                        <a href="categories/kubernetes">kubernetes</a>
                    </li>
                    
                    <li>
                        <a href="categories/tech">tech</a>
                    </li>
                    

                    
		            <li>
                        <a href="search">SEARCH <img src="img/search.png" height="15" style="cursor: pointer;"></a>
		            </li>
                    
                </ul>
            </div>
        </div>
        
    </div>
    
</nav>
<script>
    
    
    
    var $body   = document.body;
    var $toggle = document.querySelector('.navbar-toggle');
    var $navbar = document.querySelector('#huxblog_navbar');
    var $collapse = document.querySelector('.navbar-collapse');

    $toggle.addEventListener('click', handleMagic)
    function handleMagic(e){
        if ($navbar.className.indexOf('in') > 0) {
        
            $navbar.className = " ";
            
            setTimeout(function(){
                
                if($navbar.className.indexOf('in') < 0) {
                    $collapse.style.height = "0px"
                }
            },400)
        }else{
        
            $collapse.style.height = "auto"
            $navbar.className += " in";
        }
    }
</script>




<style type="text/css">
    header.intro-header{
        background-image: url('https://res.cloudinary.com/lyp/image/upload/v1544363191/hugo/blog.github.io/743a4e9227e1f14cb24a1eb6db29e183.jpg')
    }
</style>
<header class="intro-header" >
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <div class="post-heading">
                    <div class="tags">
                       
                       <a class="tag" href="/tags/cloudnative" title="CloudNative">
                           CloudNative
                        </a>
                        
                       <a class="tag" href="/tags/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80" title="编程语言">
                           编程语言
                        </a>
                        
                       <a class="tag" href="/tags/ballerina" title="ballerina">
                           ballerina
                        </a>
                        
                       <a class="tag" href="/tags/k8s" title="k8s">
                           k8s
                        </a>
                        
                       <a class="tag" href="/tags/docker" title="docker">
                           docker
                        </a>
                        
                    </div>
                    <h1>云原生编程语言ballerina:hello-world</h1>
                    <h2 class="subheading"></h2>
                    <span  class="meta">Posted by 梁远鹏 on 2020-01-01
                        
                        <span id="busuanzi_container_page_pv">|<span id="busuanzi_value_page_pv"></span><span>
                            <span id="/post/ballerina-hello-world/" class="leancloud_visitors meta_data_item" data-flag-title="">
    <span class="post-meta-item-icon">
      <span class="octicon octicon-eye"></span> 
    </span>
    <i class="fa fa-eye"></i>
    <span class="old-visitors-count" style="display: none;"></span>
    <span class="leancloud-visitors-count"></span>
</span>






                            阅读 </span></span>|<span class="post-date">共1030字</span>，阅读约<span class="more-meta"> 3 分钟</span>
                        
                    </span>
                </div>
            </div>
        </div>
    </div>
</header>




<article>
    <div class="container">
        <div class="row">

            
            <div class="
                col-lg-8 col-lg-offset-2
                col-md-10 col-md-offset-1
                post-container">

		
                <header>
                <h2>TOC</h2>
                </header>
                <nav id="TableOfContents">
  <ul>
    <li><a href="#前言">前言</a></li>
    <li><a href="#example">Example</a></li>
  </ul>

  <ul>
    <li>
      <ul>
        <li><a href="#hello-world-main">Hello World Main</a></li>
        <li><a href="#hello-world-service">Hello World Service</a></li>
        <li><a href="#hello-world-paraller">Hello World Paraller</a></li>
        <li><a href="#hello-world-client">Hello World Client</a></li>
      </ul>
    </li>
  </ul>
</nav>
		
		<h2 id="前言">前言</h2>
<p>Ballerina是一款完全开源的编译时强类型语言,愿景是让云原生时代的程序员轻松编写出想要的的软件.<br>
开源地址:<a href="https://github.com/ballerina-platform/ballerina-lang">https://github.com/ballerina-platform/ballerina-lang</a></p>
<h2 id="example">Example</h2>
<h1 id="下载对应平台的包进行安装">下载对应平台的包进行安装</h1>
<p><a href="https://ballerina.io/downloads/">https://ballerina.io/downloads/</a></p>
<p>这里使用的是在ubuntu环境下安装，下载好deb包后，进行安装</p>
<pre><code>lan@lan-machine:~$ sudo dpkg -i ballerina-linux-installer-x64-1.1.0.deb 
[sudo] password for lan: 
Selecting previously unselected package ballerina-1.1.0.
(Reading database ... 187196 files and directories currently installed.)
Preparing to unpack ballerina-linux-installer-x64-1.1.0.deb ...
Unpacking ballerina-1.1.0 (1.1.0) ...
Setting up ballerina-1.1.0 (1.1.0) ...
lan@lan-machine:~$ ballerina version
jBallerina 1.1.0
Language specification 2019R3
Ballerina tool 0.8.0
</code></pre><p>可以看到安装的ballerina的版本是1.1.0</p>
<h3 id="hello-world-main">Hello World Main</h3>
<ol>
<li>创建ballerina目录以及进入到ballerina目录下，这一步骤不是必须的，但为了文件整理，建议这样处理</li>
</ol>
<pre><code>lan@lan-machine:/disk/note$ mkdir ballerina &amp;&amp; cd ballerina
lan@lan-machine:/disk/note/ballerina$
</code></pre><ol start="2">
<li>创建hello-world.bal文件并写入hello-world print对应代码 <br>
<code>lan@lan-machine:/disk/note/ballerina$ touch hello-world.bal</code></li>
</ol>
<pre><code>import ballerina/io;
public function main() {
    io:println(&quot;Hello, World!&quot;);
}
</code></pre><ol start="3">
<li>运行hello-world</li>
</ol>
<pre><code>lan@lan-machine:/disk/note/ballerina$ ballerina run hello-world.bal 
Compiling source
        hello-world.bal

Generating executables
Running executables

Hello, World!  
</code></pre><p>可以看到，已经成功输入 Hello World,最简单的一个例子完成了。</p>
<h3 id="hello-world-service">Hello World Service</h3>
<p>第二个hello-world的例子是启动一个监听9090端口的http服务器</p>
<ol>
<li>创建hello-world-service.bal文件并写入对应的代码</li>
</ol>
<p><code>lan@lan-machine:/disk/note/ballerina$ touch hello-world-service.bal</code></p>
<pre><code>import ballerina/http;
import ballerina/log;
service hello on new http:Listener(9090) {

    resource function sayHello(http:Caller caller, http:Request req) {

        var result = caller-&gt;respond(&quot;Hello, World!&quot;);

        if (result is error) {
            log:printError(&quot;Error sending response&quot;, result);
        }
    }
}
</code></pre><ol>
<li>运行hello-world-service.bal</li>
</ol>
<pre><code>lan@lan-machine:/disk/note/ballerina$ ballerina run hello-world-service.bal
Compiling source
        hello-world-service.bal

Generating executables
Running executables

[ballerina/http] started HTTP/WS listener 0.0.0.0:9090

lan@lan-machine:~$ curl http://localhost:9090/hello/sayHello
Hello, World!
</code></pre><p>其中路径的组成是这样的，</p>
<ol>
<li>http:Listener(9090) 构成监听端口9090,也就是localhost:9090</li>
<li>service hello 构成第一个路径</li>
<li>resource function sayHello 构成第二个路径</li>
</ol>
<p>完整的组成之后就是http://localhost:9090/hello/sayHello</p>
<h3 id="hello-world-paraller">Hello World Paraller</h3>
<p>第三个hello-world的例子是异步任务的执行</p>
<ol>
<li>创建hello-world-paraller.bal文件并写入对应代码<br>
<code>lan@lan-machine:/disk/note/ballerina$ touch hello-world-paraller.bal</code></li>
</ol>
<pre><code>import ballerina/io;
public function main() {
    worker w1 {
        io:println(&quot;Hello, World! #m&quot;);
    }

    worker w2 {
        io:println(&quot;Hello, World! #n&quot;);
    }
    worker w3 {
        io:println(&quot;Hello, World! #k&quot;);
    }
}
</code></pre><ol>
<li>运行hello-world-paraller.bal 文件</li>
</ol>
<pre><code>lan@lan-machine:/disk/note/ballerina$ ballerina run hello-world-paraller.bal
Compiling source
        hello-world-paraller.bal

Generating executables
Running executables

Hello, World! #m
Hello, World! #n
Hello, World! #k
lan@lan-machine:/disk/note/ballerina$ 
lan@lan-machine:/disk/note/ballerina$ 
lan@lan-machine:/disk/note/ballerina$ ballerina run hello-world-paraller.bal
Compiling source
        hello-world-paraller.bal

Generating executables
Running executables

Hello, World! #n
Hello, World! #m
Hello, World! #k
</code></pre><p>可以看到每次打印的顺序并不是一样的</p>
<h3 id="hello-world-client">Hello World Client</h3>
<p>最后一个hello-world的例子是http客户端请求</p>
<ol>
<li>创建hello-world-client.bar文件并写入对应代码
<code>lan@lan-machine:/disk/note/ballerina$ touch hello-world-client.bal</code></li>
</ol>
<pre><code>import ballerina/http;
import ballerina/io;
public function main() {
    http:Client clientEP = new (&quot;http://www.mocky.io&quot;);

    var resp = clientEP-&gt;get(&quot;/v2/5ae082123200006b00510c3d/&quot;);

    if (resp is http:Response) {
        var payload = resp.getTextPayload();
        if (payload is string) {

            io:println(payload);
        } else {

            io:println(payload.detail());
        }
    } else {

        io:println(resp.detail());
    }
}
</code></pre><ol start="2">
<li>运行hello-world-client.bal文件</li>
</ol>
<pre><code>lan@lan-machine:/disk/note/ballerina$ ballerina run hello-world-client.bal
Compiling source
        hello-world-client.bal

Generating executables
Running executables
</code></pre><p>Hello World</p>
<p>例子中是请求 <code>http://www.mocky.io/v2/5ae082123200006b00510c3d/</code> 并将结果打印出来</p>
<p>更多的example可以在<a href="https://ballerina.io/v1-1/learn/by-example/">官网</a>找到</p>


        <h2>微信公众号</h2>
<p>扫描下面的二维码关注我们的微信公众号,第一时间查看最新内容。同时也可以关注我的Github，看看我都在了解什么技术，在页面底部可以找到我的Github。</p>
<img src="https://res.cloudinary.com/lyp/image/upload/v1581729955/hugo/blog.github.io/qrcode_for_sikekafeidou.jpg" alt="wechat-account-qrcode">


                <hr>

                <ul class="pager">
                    
                    <li class="previous">
                        <a href="/post/remote-deploy-of-jenkins-for-linux-application/" data-toggle="tooltip" data-placement="top" title="Jenkins远程部署Linux服务器">&larr; Jenkins远程部署Linux服务器</a>
                    </li>
                    
                    
                    <li class="next">
                        <a href="/post/deploy-minio/" data-toggle="tooltip" data-placement="top" title="docker部署minio">docker部署minio &rarr;</a>
                    </li>
                    
                </ul>

            
<script src="https://utteranc.es/client.js"
repo="liangyuanpeng/liangyuanpeng.github.io"
issue-term="title"
theme="github-light"
crossorigin="anonymous"
async>
</script>



            </div>

            


            
            <div class="
                col-lg-8 col-lg-offset-2
                col-md-10 col-md-offset-1
                sidebar-container">

                
                <section>
                    <hr class="hidden-sm hidden-xs">
                    <h5><a href="/tags/">FEATURED TAGS</a></h5>
                    <div class="tags">
                     
                    
                        
                    
                        
                    
                        
                    
                        
                    
                        
                    
                        
                    
                        
                            <a href="/tags/blog" title="blog">
                                blog
                            </a>
                        
                    
                        
                    
                        
                            <a href="/tags/chirpstack" title="chirpstack">
                                chirpstack
                            </a>
                        
                    
                        
                    
                        
                            <a href="/tags/ci/cd" title="ci/cd">
                                ci/cd
                            </a>
                        
                    
                        
                    
                        
                            <a href="/tags/cloudnative" title="cloudnative">
                                cloudnative
                            </a>
                        
                    
                        
                            <a href="/tags/cncf" title="cncf">
                                cncf
                            </a>
                        
                    
                        
                    
                        
                            <a href="/tags/docker" title="docker">
                                docker
                            </a>
                        
                    
                        
                            <a href="/tags/docker-compose" title="docker-compose">
                                docker-compose
                            </a>
                        
                    
                        
                    
                        
                    
                        
                    
                        
                            <a href="/tags/fluentd" title="fluentd">
                                fluentd
                            </a>
                        
                    
                        
                    
                        
                    
                        
                            <a href="/tags/halo" title="halo">
                                halo
                            </a>
                        
                    
                        
                            <a href="/tags/hugo" title="hugo">
                                hugo
                            </a>
                        
                    
                        
                            <a href="/tags/image" title="image">
                                image
                            </a>
                        
                    
                        
                    
                        
                            <a href="/tags/iot" title="iot">
                                iot
                            </a>
                        
                    
                        
                    
                        
                    
                        
                    
                        
                    
                        
                    
                        
                    
                        
                            <a href="/tags/kubernetes" title="kubernetes">
                                kubernetes
                            </a>
                        
                    
                        
                    
                        
                    
                        
                    
                        
                            <a href="/tags/lorawan" title="lorawan">
                                lorawan
                            </a>
                        
                    
                        
                    
                        
                    
                        
                    
                        
                            <a href="/tags/middleware" title="middleware">
                                middleware
                            </a>
                        
                    
                        
                    
                        
                            <a href="/tags/mq" title="mq">
                                mq
                            </a>
                        
                    
                        
                    
                        
                    
                        
                    
                        
                    
                        
                            <a href="/tags/ops" title="ops">
                                ops
                            </a>
                        
                    
                        
                    
                        
                            <a href="/tags/prometheus" title="prometheus">
                                prometheus
                            </a>
                        
                    
                        
                    
                        
                    
                        
                            <a href="/tags/rpc" title="rpc">
                                rpc
                            </a>
                        
                    
                        
                    
                        
                            <a href="/tags/sofa" title="sofa">
                                sofa
                            </a>
                        
                    
                        
                            <a href="/tags/sofastack" title="sofastack">
                                sofastack
                            </a>
                        
                    
                        
                            <a href="/tags/springboot" title="springboot">
                                springboot
                            </a>
                        
                    
                        
                    
                        
                    
                        
                    
                        
                    
                        
                    
                        
                    
                        
                    
                        
                    
                        
                    
                        
                    
                        
                    
                        
                    
                        
                    
                        
                    
                        
                    
                        
                    
                        
                    
                    </div>
                </section>



               

                
                
            </div>

            
            
            <div class="
            col-lg- col-lg-offset-2
            col-md-10 col-md-offset-1
            ">
            <section>
                <hr class="hidden-sm hidden-xs">
                

            </section>
            </div>
            


        </div>
    </div>
</article>






<footer>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <ul class="list-inline text-center">
                   
                    
                    <li>
                        <a target="_blank" href="https://github.com/liangyuanpeng">
                            <span class="fa-stack fa-lg">
                                <i class="fa fa-circle fa-stack-2x"></i>
                                <i class="fa fa-github fa-stack-1x fa-inverse"></i>
                            </span>
                        </a>
                    </li>
                   
                    
                    <li>
                        <a href="mailto:liangyuanpengem@163.com">
                            <span class="fa-stack fa-lg">
                                <i class="fa fa-circle fa-stack-2x"></i>
                                <i class="fa fa-envelope fa-stack-1x fa-inverse"></i>
                            </span>
                        </a>
                    </li>
		    

                    
                    <li>
                        <a href="https://twitter.com/lan31793328">
                            <span class="fa-stack fa-lg">
                                <i class="fa fa-circle fa-stack-2x"></i>
                                <i class="fa fa-twitter fa-stack-1x fa-inverse"></i>
                            </span>
                        </a>
                    </li>
                    
                    
                    <li>
                        <a target="_blank" href="https://www.zhihu.com/people/liangyuanpeng">
                            <span class="fa-stack fa-lg">
                                <i class="fa fa-circle fa-stack-2x"></i>
                                <i class="fa  fa-stack-1x fa-inverse">知</i>
                            </span>
                        </a>
                    </li>
		    
                    
                    <li>
                        <a target="_blank" href="https://weibo.com/u/1908782280">
                            <span class="fa-stack fa-lg">
                                <i class="fa fa-circle fa-stack-2x"></i>
                                <i class="fa fa-weibo fa-stack-1x fa-inverse"></i>
                            </span>
                        </a>
                    </li>
		    

                    

		    
                    
                    
                </ul>
		<p class="copyright text-muted">
                    Copyright &copy; Hi,I`m lan , 2020
                    <br>
                    <a href="https://themes.gohugo.io/hugo-theme-cleanwhite">CleanWhite Hugo Theme</a> by <a href="https://zhaohuabing.com">Huabing</a> |
                    <iframe
                        style="margin-left: 2px; margin-bottom:-5px;"
                        frameborder="0" scrolling="0" width="100px" height="20px"
                        src="https://ghbtns.com/github-btn.html?user=zhaohuabing&repo=hugo-theme-cleanwhite&type=star&count=true" >
                    </iframe>
                </p>
            </div>
        </div>
    </div>
</footer>




<script>
    function async(u, c) {
      var d = document, t = 'script',
          o = d.createElement(t),
          s = d.getElementsByTagName(t)[0];
      o.src = u;
      if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
      s.parentNode.insertBefore(o, s);
    }
</script>






<script>
    
    if($('#tag_cloud').length !== 0){
        async("/js/jquery.tagcloud.js",function(){
            $.fn.tagcloud.defaults = {
                
                color: {start: '#bbbbee', end: '#0085a1'},
            };
            $('#tag_cloud a').tagcloud();
        })
    }
</script>


<script>
    async("https://cdnjs.cloudflare.com/ajax/libs/fastclick/1.0.6/fastclick.js", function(){
        var $nav = document.querySelector("nav");
        if($nav) FastClick.attach($nav);
    })
</script>







<script>
    
    var _baId = 'fad9c137f8ce239f9b323d36c871f8e6';

    
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?" + _baId;
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
</script>







</body>
</html>
